import WxValidate from '../../utils/WxValidate.js'

const app = getApp()

let Validate = ""

Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      name: '',
      mobile: '',
      address: '',
      remark: '',
      history: ''
    },
    tabArr: {
      curHdIndex: -1
    },
    stars: [0, 1, 2, 3, 4],
    normalSrc: '../../assets/start.svg',
    selectedSrc: '../../assets/selectStart.svg',
    halfSrc: '../../assets/selectStart.png',
    code: "获取验证码",
    _timer: 60,
    mobile: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    const rules = {
      name: {
        required: true
      },
      mobile: {
        required: true,
        tel: true
      },
      address: {
        required: true
      }
    }

    const messages = {
      name: {
        required: '请输入姓名',
      },
      mobile: {
        required: '请输入手机号码',
        tel: '号码格式不正确'
      },
      address: {
        required: '请输入地址',
      }
    }
    Validate = new WxValidate(rules, messages)
    wx.setNavigationBarTitle({
      title: !options.title ? '新建线索' : '编辑线索',
    })
    this.setData({
      options: !options.title ? false : JSON.parse(options.title),
      index: !options.title ? 'load' : options.num,
    })
    console.log(JSON.parse(options.title))
  },
  chooseicon: function (e) {
    var strnumber = e.target.dataset.id;
    var _obj = {};
    _obj.curHdIndex = strnumber;
    this.setData({
      tabArr: _obj
    });

  },
  handleHistory(e){
    // var strnumber = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../clues/history/history?title=' + e.currentTarget.dataset.history,
    })
  },
  updateClues(e, options, index){
    if (!!e.detail.value.remark) {
      e.detail.value.remark = options.history +  "~"+ e.detail.value.remark
    }
    app.query('/api/clues/update_me', { id: options.id,clue: e.detail.value }, 'post').then(res => {
      console.log(res)
      if (res.status == 'success') {
        app.showToast('线索更新成功', 'success', 2000)
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
          // wx.redirectTo({
          //   url: '../cluse/clues?num='
          // })
        }, 600)


      } else {
        app.showToast(res.msg[0], false, 500, '../../assets/warning.png')
      }
    });
  },
  postClues(e, index){
    app.query('/api/clues', { clue: e.detail.value }, 'post').then(res => {
      // console.log(res)
      if (res.status == 'success') {
        app.showToast('新建线索成功', 'success', 3000)
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 600)
        // wx.navigateTo({
        //   url: '../reaspon/reaspon?title=clues'
        // })
      }else{
        app.showToast(res.msg[0], false, 500, '../../assets/warning.png')
      }
    });
  },
  formSubmit: function (e) {
    const params = e.detail.value
    const { options, index } = this.data

    let _remark = { "entityMap": {}, "blocks": [{ "key": "1k137", "text": "123", "type": "unstyled", "depth": 0, "inlineStyleRanges": [], "entityRanges": [], "data": {} }] };
    let _random;
    if (!Validate.checkForm(e)) {

      const error = Validate.errorList[0]
      app.showToast(error.msg, false, 500, '../../assets/warning.png')
      //提示信息  
      // console.log(error, e)
      return false
    }

    if (!!e.detail.value.remark) {
      _remark.blocks[0].key = Math.floor(Math.random() * 1000)
      _remark.blocks[0].text = e.detail.value.remark;
      _remark.date = new Date();
      _remark.name = wx.getStorageSync('current_user').name
      e.detail.value.remark = JSON.stringify(_remark);
    }

    if(!!options.name){
      this.updateClues(e, options, index)
    }else{
      this.postClues(e, index)
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})